<?php
/**
 * Enter description here ...
 * @author NgoAnhTu
 * @date 2013-10-16
 */
class AlbumBusiness {
	
	const ALBUM_THUMB_WIDTH = 80;
	const ALBUM_THUMB_HEIGHT = 60;
	
	const IMG_THUMB_WIDTH = 130;
	const IMG_THUMB_HEIGHT = 90;
	const IMG_WIDTH = 693;
	
	private static $instance = null;
	
	/**
	 * Enter description here ...
	 * @var AlbumDAO
	 */
	private $albumDAO;
	
	/**
	 * Enter description here ...
	 * @var AlbumDetailDAO
	 */
	private $albumDetailDAO;
	
	/**
	 * Enter description here ...
	 * @var PictureDAO
	 */
	private $pictureDAO;
		
	/**
	 * AlbumBusiness instance only
	 * @return AlbumBusiness
	 */
	public static function getInstance() {
		if (self::$instance == null)
			self::$instance = new AlbumBusiness();
		return self::$instance;
	}
		
	/**
	 * Constructor
	 */
	public function AlbumBusiness() {
		$this->albumDAO = AlbumDAO::getInstance();
		$this->albumDetailDAO = AlbumDetailDAO::getInstance();
		$this->pictureDAO = PictureDAO::getInstance();
	}
	
	/**
	 * @return Ambigous <array<index,, multitype:Ambigous <Entity, NULL> >
	 */
	public function findAll($pageIndex = 1, $pattern = '') {
		return $this->albumDAO->selectPage($pageIndex, DAO::PAGE_SMALL, "Name LIKE '$pattern%'", "Name");
	}
	
	/**
	 * Enter description here ...
	 * @param array $idArray
	 */
	public function active($idArray) {
		$ids = "''";
		if (sizeof($idArray) > 0) {
			foreach ($idArray as $id) {
				$ids .= ",'$id'";
			}
		}
		$this->albumDAO->active($ids);
	}
	
	/**
	 * Check exist Album 
	 * @param string $id
	 * @param string $name
	 * @return boolean
	 */
	public function isExist($name) {
		return $this->albumDAO->isExist(new Album(Entity::NEW_ID, $name));
	}

	/**
	 * Check exist Album 
	 * @param unknown_type $id
	 * @param unknown_type $name
	 * @return boolean
	 */
	public function isExistOther($id, $name) {
		return $this->albumDAO->isExistOther(new Album($id, $name));
	}

	/**
	 * Enter description here ...
	 * @param unknown_type $name
	 * @param unknown_type $image
	 * @return Ambigous <Entity, NULL>
	 */
	public function create($name, $image, $names, $codes) {
		$album = new Album(Entity::NEW_ID, $name);
		$this->albumDAO->beginTransaction();
		
		$image = new Image($image);
		if ($image->size > 0) {
			$album->image = $image->thumbnail(Properties::ALBUM_DIR, self::ALBUM_THUMB_WIDTH, self::ALBUM_THUMB_HEIGHT, String::firstWork($name) . '.' .Image::EXT_PNG, false, 10);
			$image->destroy();
		}
		
		$album = $this->albumDAO->insert($album);
		if ($album != null) {
			for ($i = 0; $i < sizeof($names); $i++) {
				if ($this->albumDetailDAO->insert(new AlbumDetail(Entity::NEW_ID, $album->id, $codes[$i], $names[$i])) == null) {
					$this->albumDetailDAO->rollback();
					return null;
				}
			}
		}
		
		$this->albumDAO->commit();
		return $album;
	}
	
	/**
	 * Enter description here ...
	 * @param Album $album
	 * @param Image $image
	 * @param array $names
	 * @param array $codes
	 */
	public function update($album, $image, $names, $codes) {
		$oldImage = null;
		$newImage = null;
		$image = new Image($image);
		if ($image->size > 0) {
			$oldImage = $album->image;
			$album->image = $image->thumbnail(Properties::ALBUM_DIR, self::ALBUM_THUMB_WIDTH, self::ALBUM_THUMB_HEIGHT, String::filter(String::firstWork($album->name)) . '.' .Image::EXT_PNG, false, 10);
			$newImage = $album->image;
			if ($oldImage == $newImage) {
				$oldImage = null;
			}
			$image->destroy();
		}
		
		$this->albumDAO->beginTransaction();
		$album = $this->albumDAO->update($album);
		
		if ($album != null) {
			for ($i = 0; $i < sizeof($names); $i++) {
				$detail = $this->albumDetailDAO->getDetail($album->id, $codes[$i]);
				if ($detail != null) {
					$detail->content = $names[$i];
					if ($this->albumDetailDAO->update($detail) == null) {
						$this->albumDetailDAO->rollback();
						$image->del($newImage);	
						return null;
					}
				} else {
					$detail = new AlbumDetail(Entity::NEW_ID, $album->id, $codes[$i], $names[$i]);
					if ($this->albumDetailDAO->insert($detail) == null) {
						$this->albumDetailDAO->rollback();
						$image->del($newImage);	
						return null;
					}
				}
			}
			$image->del($oldImage);	
		}
		
		$this->albumDAO->commit();
		return $album;
	}
	
	/**
	 * Find by Album code
	 * @param string $id
	 * @return Album
	 */
	public function findById($id) {
		$album = $this->albumDAO->selectById(new Album($id));
		if ($album != null) {
			$album->details = $this->albumDetailDAO->getDetails($id);
		}
		return $album;
	}
	
	/**
	 * Enter description here ...
	 * @param unknown_type $id
	 * @return Ambigous <boolean, resource>
	 */
	public function del($id) {
		return $this->albumDAO->delete(new Album($id));
	} 
	
	/**
	 * Enter description here ...
	 * @param integer $albumId
	 */
	public function getPics($albumId) {
		return $this->pictureDAO->select(DAO::FIRST_PAGE, DAO::MAX_SIZE, "AlbumId = $albumId", 'Order');
	}
	
	/**
	 * Enter description here ...
	 * @param integer $albumId
	 * @param array $orders
	 * @param array $imgs
	 * @return boolean
	 */
	public function insertPics($albumId, $orders, $imgs) {
		$count = 0;
		for ($i = 0; $i < sizeof($imgs["name"]); $i++) {
			$image = new Image($imgs, $i);
			if ($image->size > 0) {
				$order = $orders[$i];
				$url = $image->saveAs(Properties::ALBUM_DIR, $order . '.' . Image::EXT_PNG, self::IMG_WIDTH, $isReplace = false);
				$thumbUrl = str_replace(Properties::ALBUM_DIR . '/', '', $url);
				$image = new Image($imgs, $i);
				$thumbUrl = $image->thumbnail(Properties::ALBUM_DIR, self::IMG_THUMB_WIDTH, self::IMG_THUMB_HEIGHT, "thumb_$thumbUrl", false, 10);
				if ($url != null) {
					$image->destroy();
					$pic = new Picture(0, $thumbUrl, $url, $order, $albumId);
					if ($this->pictureDAO->insert($pic) != null) {
						$count++;
					}
				}
			}
		}
		return $count;
	}
	
	/**
	 * Enter description here ...
	 * @param array $picIDs
	 * @param array $orders
	 */
	public function updatePicOrders($picIDs, $orders) {
		$check = true;
		$this->pictureDAO->beginTransaction();
		
		for ($i = 0; $i < sizeof($picIDs); $i++) {
			$pic = $this->pictureDAO->selectById(new Picture($picIDs[$i]));
			$pic->order = $orders[$i];
			if ($this->pictureDAO->update($pic) == null) {
				$this->pictureDAO->rollback();
				$check = false;
				break;
			}
		}
		
		$this->pictureDAO->commit();
		return $check;
	}
	
	/**
	 * Enter description here ...
	 * @param array $delIDs
	 */
	public function deletePics($delIDs) {
		$check = true;
		$this->pictureDAO->beginTransaction();
		$img = new Image();
		
		for ($i = 0; $i < sizeof($delIDs); $i++) {
			$pic = $this->pictureDAO->selectById(new Picture($delIDs[$i]));
			if ($pic != null) {
				if (!$this->pictureDAO->delete(new Picture($delIDs[$i]))) {
					$this->pictureDAO->rollback();
					$check = false;
					break;
				} else {
					$img->del($pic->image);
					$img->del($pic->thumb);
				}
			}
		}
		
		$this->pictureDAO->commit();
		return $check;		
	}
	
}
?>